$(function() {
	var symptomArray = $("#symptomArray"), propArray = $("#propArray"), allFields = $(
			[]).add(symptomArray).add(propArray), validateTips = $(".validateTips");

	function updateTips(t) {
		validateTips.text(t).addClass("ui-state-highlight");
		setTimeout(function() {
			validateTips.removeClass("ui-state-highlight", 1500);
		}, 500);
	}

	function checkLength(o, n, min, max) {
		if (o.val().length > max || o.val().length < min) {
			o.addClass("ui-state-error");
			updateTips("" + n + " 的长度必须在 " + min + " 和 " + max + " 之间。");
			return false;
		} else {
			return true;
		}
	}

	function checkEmpity(o, n) {
		if (o.val() != null && o.val().length > 0) {
			return true
		} else {
			o.addClass("ui-state-error");
			updateTips("" + n + " 不能为空 ");
			return false;
		}
	}

	function checkRegexp(o, regexp, n) {
		if (!(regexp.test(o.val()))) {
			o.addClass("ui-state-error");
			updateTips(n);
			return false;
		} else {
			return true;
		}
	}
	$("#dialog-form").dialog(
			{
				create : function(event, ui) {
					$(event.target).parent().css('position', 'fixed');
				},
				resizeStop : function(event, ui) {
					var position = [
							(Math.floor(ui.position.left) - $(window)
									.scrollLeft()),
							(Math.floor(ui.position.top) - $(window)
									.scrollTop()) ];
					$(event.target).parent().css('position', 'fixed');
					$(dlg).dialog('option', 'position', position);
				},
				autoOpen : false,
				height : 350,
				width : 550,
				modal : true,
				buttons : {
					"确认编辑" : function() {
						var bValid = true;
						allFields.removeClass("ui-state-error");
						// 开始校验
						bValid = bValid
								&& checkEmpity(symptomArray, "symptomArray");
						bValid = bValid && checkEmpity(propArray, "propArray");
						// 如果校验成功
						if (bValid) {
							var param = {
								"id" : $("#id").val(),
								"symptomArray" : symptomArray.val(),
								"propArray" : propArray.val()
							};
							API.editSymptomReveal(param, function(res) {
								if (res.code == '0') {
									window.location.reload();
								} else {
									alert(res.desc);
								}
							});
						}
					},
					"取消" : function() {
						$(this).dialog("close");
					}
				},
				close : function() {
					allFields.val("").removeClass("ui-state-error");
				}
			});
	$(".button").button();
});
function editReveal(id, symptomArray, propArray) {
	$("#id").val(id);
	$("#symptomArray").val(symptomArray);
	$("#propArray").val(propArray);
	$("#dialog-form").dialog("open");
}
function updateRevealStatus(id, status) {
	var param = {
		"id" : id,
		"revealStatus" : status
	}
	API.updateSymptomRevealStatus(param, function(res) {
		if (res.code == '0') {
			window.location.reload();
		} else {
			alert(res.desc);
		}
	});
}
function changeRevealWithSynonym(id, symptomStd, synonym) {
	var param = {
		"id" : id,
		"symptomStd" : symptomStd,
		"synonym" : synonym,
	}
	API.changeRevealWithSynonym(param, function(res) {
		if (res.code == '0') {
			window.location.reload();
		} else {
			alert(res.desc);
		}
	});
}